<template>
  <!-- 手工录入 -->
  <Drawer
    title="手工录入"
    :visible="manualInputVisible"
    width="40%"
    :footer-btns="[
      {
        text: '确定',
        type: 'primary',
        trigger: 'confirm',
      },
      {
        text: '取消',
        type: 'info',
        trigger: 'cancel',
      },
    ]"
    @cancel="handleCancel"
    @confirm="submitForm('ruleForm', 'hospitalizationForm')"
  >
    <div class="drawer-box">
      <p style="display: flex; justify-content: space-between">
        <span>患者信息</span>
        <span
          v-if="!ManualInputEditpreId"
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <el-input
            v-model="inputIdenno"
            size="mini"
            style="width: 180px; margin-right: 15px"
            placeholder="请输入身份证号/历史挂号单"
          />
          <Button style="margin: 0" @click="prescriptionNumber">查询</Button>
        </span>
      </p>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-position="right"
        class="demo-ruleForm"
        label-width="120px"
        v-if="manualInputVisible"
      >
      <div class="formitem">
        <el-form-item label="身份证件类型" prop="idenType">
          <el-radio-group v-model="ruleForm.idenType">
            <el-radio v-for="(item, index) in typeList" :key="index" :label="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
        <div class="formitem">
          <el-form-item label="证件号" prop="idenNo">
            <el-input
              v-model="ruleForm.idenNo"
              size="mini"
              style="width: 180px"
              :disabled="idenNodisabled"
            />
          </el-form-item>
          <el-form-item label="出生年月" prop="birthday">
            <el-date-picker
              v-model="ruleForm.birthday"
              type="datetime"
              placeholder="选择出生日期"
              default-time="00:00:00"
              size="mini"
              style="width: 180px"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="病人姓名" prop="name">
            <el-input
              v-model="ruleForm.name"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select
              v-model="ruleForm.sex"
              placeholder="请选择性别"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option label="男" value="1" />
              <el-option label="女" value="2" />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="结算类型" prop="paykindCode" style="width: 350px;">
            <!-- <el-select
              v-model="ruleForm.paykindCode"
              placeholder="请选择结算类型"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.paykindCodeoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select> -->
            <!--  -->
            <el-radio-group v-model="ruleForm.paykindCode">
              <el-radio
                :label="item.value"
                v-for="(item, index) in settlementList"
                :key="index"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="国籍" prop="counCode">
            <el-select
              v-model="ruleForm.counCode"
              placeholder="请选择国籍"
              size="mini"
              style="width: 180px"
              nopmable
              clearable
            >
              <el-option label="中国" value="cn" />
              <el-option label="其它" value="500" />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="籍贯" prop="dist">
            <el-input
              v-model="ruleForm.dist"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
          <el-form-item label="现居地" prop="home">
            <el-input v-model="ruleForm.home" size="mini" />
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="联系电话" prop="homeTel">
            <el-input
              v-model="ruleForm.homeTel"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
          <el-form-item label="工作单位" prop="workName">
            <el-input
              v-model="ruleForm.workName"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="联系人" prop="linkmaName">
            <el-input
              v-model="ruleForm.linkmaName"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
          <el-form-item label="与联系人关系" prop="relaCode">
            <el-select
              v-model="ruleForm.relaCode"
              placeholder="请选择联系人关系"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.relativeoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="联系人电话" prop="linkmanTel">
            <el-input
              v-model="ruleForm.linkmanTel"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
        </div>
        <el-form-item label="联系人地址" prop="linkmanAdd">
          <el-input v-model="ruleForm.linkmanAdd" size="mini" />
        </el-form-item>
      </el-form>
      <p>住院信息</p>
      <el-form
        ref="hospitalizationForm"
        :model="hospitalizationForm"
        :rules="hospitalizationrules"
        label-position="right"
        class="demo-ruleForm"
        label-width="120px"
        v-if="manualInputVisible"
      >
        <div class="formitem">
          <el-form-item label="住院科室" prop="deptCode">
            <el-select
              v-model="hospitalizationForm.deptCode"
              placeholder="请选择住院科室"
              size="mini"
              style="width: 180px"
              filterable
              clearable
              @change="deptChange"
            >
              <el-option
                v-for="item in hospitalizationSelect.departmentoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="病区" prop="nurseCellCode">
            <el-select
              v-model="hospitalizationForm.nurseCellCode"
              placeholder="请选择病区"
              size="mini"
              style="width: 180px"
              filterable
              clearable
              @change="wardChange"
            >
              <el-option
                v-for="item in hospitalizationSelect.inpatientAreaoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="专业组" prop="medicalGroupCode" :rules="newCode === '1100' || newCode === '1140' ? medicalGroupCode : []">
            <el-select
              v-model="hospitalizationForm.medicalGroupCode"
              placeholder="请选择专业组"
              size="mini"
              style="width: 180px"
              filterable
              clearable
              @change="medicalGroupChange"
            >
              <el-option
                v-for="item in hospitalizationSelect.professionalList"
                :key="item.medicalGroupCode"
                :label="item.medicalGroupName"
                :value="item.medicalGroupCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="医疗组长" prop="medicalGroupLeaderCode"  :rules="newCode === '1100' || newCode === '1140' ? medicalGroupLeaderCode : []">
            <el-select
              v-model="hospitalizationForm.medicalGroupLeaderCode"
              placeholder="请选择医疗组长"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.medicalGroupList"
                :key="item.medicalGroupLeaderCode"
                :label="item.medicalGroupLeaderName"
                :value="item.medicalGroupLeaderCode"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="床号" prop="bedNo">
            <el-select
              v-model="hospitalizationForm.bedNo"
              placeholder="请选择床号"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="(item,index) in hospitalizationSelect.bedNumber"
                :key="index"
                :label="item.name"
                :value="item.name"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="期望入院时间" prop="preDate">
            <el-date-picker
              v-model="hospitalizationForm.preDate"
              type="datetime"
              placeholder="选择期望入院时间"
              default-time="00:00:00"
              size="mini"
              style="width: 180px"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="主管医生" prop="predoctCode">
            <el-select
              v-model="hospitalizationForm.predoctCode"
              placeholder="请选择主管医生"
              size="mini"
              style="width: 180px"
              :filterable="predoctfilterable"
              :remote="predoctremote"
              :remote-method="remoteMethod"
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.predoctCodeoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="入院诊断" prop="diagCode">
            <el-select
              v-model="hospitalizationForm.diagCode"
              placeholder="请选择入院诊断"
              size="mini"
              style="width: 180px"
              :filterable="predoctfilterable"
              :remote="prediagremote"
              :remote-method="DisgnosisChange"
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.diagCodeoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="入院诊断2" prop="diagCode2">
            <el-select
              v-model="hospitalizationForm.diagCode2"
              placeholder="请选择入院诊断"
              size="mini"
              style="width: 180px"
              :filterable="predoctfilterable"
              :remote="prediagremote"
              :remote-method="DisgnosisChange"
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.diagCodeoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="入院诊断3" prop="diagCode3">
            <el-select
              v-model="hospitalizationForm.diagCode3"
              placeholder="请选择入院诊断"
              size="mini"
              style="width: 180px"
              :filterable="predoctfilterable"
              :remote="prediagremote"
              :remote-method="DisgnosisChange"
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.diagCodeoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="是否日间手术" prop="ifRjbf">
            <el-radio-group v-model="hospitalizationForm.ifRjbf">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="入院情况" prop="inState">
            <el-select
              v-model="hospitalizationForm.inState"
              placeholder="请选择入院情况"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.daySurgeryoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="手术名称">
            <el-select
              v-model="hospitalizationForm.preOpcode"
              filterable
              remote
              reserve-keyword
              placeholder="请输入手术名称"
              :remote-method="handleRemoteName"
              :loading="nameLoading"
              size="mini"
              @change="handleNameChange"
            >
              <el-option
                v-for="item in nameList"
                :key="item.preOpcode"
                :label="item.preOpname"
                :value="item.preOpcode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手术级别">
            <!-- <el-select
              v-model="hospitalizationForm.preOpclass"
              placeholder="请选择手术级别"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in nameList"
                :key="item.preOpcode"
                :label="item.preOpclass"
                :value="item.preOpcode"
              />
            </el-select> -->
            <el-input
              v-model="hospitalizationForm.preOpclass"
              size="mini"
              disabled
              style="width: 180px"
            />
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="是否为新技术">
            <el-radio-group v-model="hospitalizationForm.ynNewtech">
              <el-radio label="是">是</el-radio>
              <el-radio label="否">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否跨科收治">
            <el-radio-group v-model="hospitalizationForm.ynOtherward">
              <el-radio label="是">是</el-radio>
              <el-radio label="否">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="患者类型" prop="patientTag">
            <el-select
              v-model="hospitalizationForm.patientTag"
              placeholder="请选择患者类型"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in hospitalizationSelect.patientTagoptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="是否31日内非计划在次入院"
            prop="ifInhosPlan"
            style="width: 400px"
            label-width="220px"
          >
            <el-radio-group v-model="hospitalizationForm.ifInhosPlan">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <div class="formitem">
          <el-form-item label="是否外伤入院" prop="ifInhosExternal">
            <el-radio-group v-model="hospitalizationForm.ifInhosExternal">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="需预交住院费" prop="prepayCost">
            <el-input
              v-model="hospitalizationForm.prepayCost"
              size="mini"
              style="width: 180px"
            />
          </el-form-item>
        </div>
        <el-form-item label="入院方式" prop="ryfs">
          <el-radio-group v-model="hospitalizationForm.ryfs">
            <el-radio label="1">急</el-radio>
            <el-radio label="2">平 | 步行</el-radio>
            <el-radio label="3">平 | 抬椅子</el-radio>
            <el-radio label="4">平 | 病车</el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="formitem">
          <el-form-item label="意向床位">
            <el-input v-model="hospitalizationForm.bednoWangted" size="mini" />
          </el-form-item>
          <el-form-item label="备注" prop="beizhu">
            <el-input v-model="hospitalizationForm.beizhu" size="mini" />
          </el-form-item>
        </div>
      </el-form>
    </div>
  </Drawer>
</template>
<script>
import {
  // getInsertIiPrepayinInsertSource,
  getPatientInfoByIdCard,
  insertIiPrepayin,
  editIiPrepayin,
  getThisIiPrepayin,
  getmedicalGroup,
  getmedicalGroupLeader,
  getKongBedList,
  getReadmissionDays
} from "../../utils/apis/InpatientRegistration.js";
import {
  getDeptList,
  getPatientAreaDTO,
  getDoctList,
  getDisgnosis,
  getBaseDictByIiPrepayin
} from "../../utils/apis/registrationApi";
import { preoperation } from "@/api/triLogin";
export default {
  props: {
    // 接收父组件传来的值
    manualInputVisible: Boolean,
    ManualInputEditpreId: [Number, String],
  },
  data() {
    const checkidenno = (rule, value, callback) => {
      const reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; // 大陆身份证 正则表达式
      const ga = /^([A-Z]\d{6,10}(\(\w{1}\))?)$/; // 港澳身份证 正则表达式
      const tw = /^\d{8}|^[a-zA-Z0-9]{10}|^\d{18}$/; // 台湾身份证 正则表达式
      const passport = /^([a-zA-z]|[0-9]){5,17}$/; // 护照 正则表达式
      // const val = /^[A-Z]{2}\d{6}$/
      if (!value) {
        callback(new Error("请输入证件号"));
      } else {
        if (this.ruleForm.idenType === '0') {
          if (!reg.test(value)) {
            callback(new Error("证件号格式不正确"));
          } else {
            callback();
            // 分别提取出生年月日
            let idCard = value;
            var year, month, day;
            if (idCard.length === 15) {
              year = "19" + idCard.substring(6, 8);
              month = idCard.substring(8, 10);
              day = idCard.substring(10, 12);
            } else {
              year = idCard.substring(6, 10);
              month = idCard.substring(10, 12);
              day = idCard.substring(12, 14);
            }
  
            var date = new Date(year, parseInt(month) - 1, parseInt(day));
  
            // 验证出生年月日的有效性
            if (
              date.getFullYear().toString() !== year ||
              date.getMonth() + 1 !== parseInt(month) ||
              date.getDate() !== parseInt(day)
            ) {
              return false; // 出生日期不合法
            }
  
            this.ruleForm.birthday = `${year}-${month}-${day} 00:00:00`;
          }
        } else if (this.ruleForm.idenType === '1') {
          if (!passport.test(value)) {
            callback(new Error('护照格式不正确'))
          } else {
            callback()
          }
        } else if(this.ruleForm.idenType === '2') {
          if (!ga.test(value) || !tw.test(value)) {
            callback(new Error('证件号格式不正确'))
          } else {
            callback()
          }
        } else {
          callback();
        }
      }
    };
    return {
      medicalGroupCode: [{ required: true, message: '请选择专业组', trigger: 'change' }],
      medicalGroupLeaderCode: [{ required: true, message: '请选择医疗组长', trigger: 'change' }],
      nameList: [],
      nameLoading: false,
      settlementList: [
        {
          label: "工伤(自费)",
          value: "ZF01",
        },
        {
          label: "第三方责任，要由第三方赔付(如车祸等)(自费)",
          value: "ZF02",
        },
        {
          label: "美容保健(自费)",
          value: "ZF03",
        },
        {
          label: "非以上三种类型，正常医保入院",
          value: "YB02",
        },
      ],
      // manualInputVisible: false, // 手工录入抽屉状态
      ruleForm: {
        idenType: "0", // 证件类型
        name: "", // 患者名称
        sex: "", // 性别编码 1男 2女
        paykindCode: "ZF01", // 结算类型
        birthday: "", // 出生日期
        idenNo: "", // 身份证
        counCode: "", // 国籍
        dist: "", // 籍贯
        home: "", // 现住址
        homeTel: "", // 联系电话
        linkmaName: "", // 联系人
        linkmanAdd: "", // 联系人地址
        linkmanTel: "", // 联系人电话
        relaCode: "", // 与联系人关系
        workName: "", // 工作单位
        mcardNo: "", // 社保编号
        pactCode: "", // 合同单位
        profCode: "", // 职务编码
      }, // 手工录入患者信息表单
      rules: {
        idenType: [
        { required: true, message: "请选择身份证件类型", trigger: "change" }
        ],
        birthday: [
          { required: true, message: "请选择出生日期", trigger: "change" },
        ],
        name: [{ required: true, message: "请输入病人姓名", trigger: "blur" }],
        idenNo: [{ required: true, validator: checkidenno, trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        // Idcard: [
        //   { required: true, message: '请输入身份证号', trigger: 'blur' }
        // ],
        homeTel: [
          { required: true, message: "请填写联系电话", trigger: "blur" },
        ],
        paykindCode: [
          { required: true, message: "请选择结算类型", trigger: "change" },
        ],
      }, // 手工录入患者信息表单必填非空判断
      hospitalizationForm: {
        preOpcode: "", // 手术名称
        preOpclass: "", // 手术级别
        ynNewtech: "否", // 是否为新技术
        ynOtherward: "否", // 是否跨科收治
        bednoWangted: "", // 意向床号
        deptCode: "", // 住院科室
        deptName: "", // 科室名称
        sdeptCode: "", // 预约大科
        nurseCellCode: "", // 病区?
        medicalGroupCode: "", // 专业组
        medicalGroupLeaderCode: "", // 医疗组长
        bedNo: "", // 床号
        preDate: "", // 期望入院时间
        predoctCode: "", // 主管医生?
        diagCode: "", // 诊断编码
        diagName: "", // 诊断名称
        ifRjbf: "0", // 是否日间手术
        inState: "", // 入院情况
        patientTag: "", // 院前准备
        ifInhosPlan: "0", // 是否31日内非计划再次入院
        ifInhosExternal: "0", // 是否外伤入院
        prepayCost: "1000", // 预交金
        ryfs: "2", // 入院方式
        ifWalk: "0", // 步行
        ifAmbulance: "0", // 病车
        ifChair: "0", // 抬椅
        ifEmergency: "0", // 是否急
        beizhu: "", // 备注
        diagCode2: "",
        diagCode3: "",
      }, // 手工录入住院信息表单
      inputIdenno: "", //输入身份证号
      hospitalizationSelect: {
        departmentoptions: [],
        inpatientAreaoptions: [],
        daySurgeryoptions: [],
        relativeoptions: [],
        patientTagoptions: [
          {
            label: "日间手术",
            value: "1",
          },
          {
            label: "日间输注",
            value: "4",
          },
          {
            label: "院前准备",
            value: "2",
          },
          {
            label: "普通",
            value: "0",
          },
        ],
        predoctCodeoptions: [],
        paykindCodeoptions: [],
        diagCodeoptions: [],
        professionalList: [],
        medicalGroupList: [],
        bedNumber: []
      },
      idenNodisabled: false, // 手工录入编辑的时候禁用
      hospitalizationrules: {
        deptCode: [
          { required: true, message: "请选择住院科室", trigger: "change" },
        ],
        nurseCellCode: [
          { required: true, message: "请选择病区", trigger: "change" },
        ],
        predoctCode: [
          { required: true, message: "请选择主管医生", trigger: "change" },
        ],
        preDate: [
          { required: true, message: "请选择期望入院时间", trigger: "change" },
        ],
        diagCode: [
          { required: true, message: "请选择入院诊断", trigger: "change" },
        ],

        patientTag: [
          { required: true, message: "请选择患者类型", trigger: "change" },
        ],
        inState: [
          { required: true, message: "请选择入院情况", trigger: "change" },
        ],
      },
      predoctfilterable: true,
      predoctremote: true,
      prediagremote: true,
      editnurseCellCode: "", // 编辑病区
      urlParam: {},
      typeList: [
        {
          label: '居民身份证',
          value: "0",
        },
        {
          label: '护照',
          value: "1",
        },
        {
          label: '港澳台居民身份证/通行证',
          value: "2",
        },
        {
          label: '旅行证据',
          value: "3",
        },
        {
          label: '其他',
          value: "9",
        }
      ],
      arr: [],
      newCode: ''
    };
  },
  created() {
    this.urlParam = this.$route.query;
  },
  mounted() {
    this.getDept();
    this.dictionaryList();
    this.getDisgnosisData();
  },
  methods: {
    handleNameChange(value){ // 手术名称
      this.hospitalizationForm.preOpclass   = this.nameList.find(item => item.preOpcode === value)?.preOpclass
    },
    handleRemoteName(query) {
      if (query !== "") {
        this.nameLoading = true;
        this.nameLoading = false;
        preoperation({
          preOpname: query,
        }).then((res) => {
          if (res.data.code === 200) {
            this.nameList = res.data.data;
          } else {
            this.$message.error(res.data.message);
          }
        });
      } else {
        this.nameList = [];
      }
    },
    handleClearForm() {
      for (const key in this.hospitalizationForm) {
        this.hospitalizationForm[key] = "";
      }
      for (const key in this.ruleForm) {
        this.ruleForm[key] = "";
      }
      this.hospitalizationForm.ynNewtech = "否"; // 是否为新技术
      this.hospitalizationForm.ynOtherward = "否"; // 是否跨科收治

      this.inputIdenno = "";
      this.hospitalizationForm.ifRjbf = "0";
      this.hospitalizationForm.ifInhosPlan = "0";
      this.hospitalizationForm.ifInhosExternal = "0";
      this.hospitalizationForm.prepayCost = "1000";
      this.hospitalizationForm.ryfs = "2";
      this.hospitalizationForm.ifWalk = "0";
      this.hospitalizationForm.ifAmbulance = "0";
      this.hospitalizationForm.ifChair = "0";
      this.hospitalizationForm.ifEmergency = "0";
      this.hospitalizationForm.beizhu = "";
      this.ruleForm.paykindCode = "ZF01";
      this.ruleForm.idenType = "0";
      this.nameList = []
      this.idenNodisabled = false;
    },
    handleCancel() {
      this.$emit("update:ManualInputEditpreId", "");
      this.$emit("update:manualInputVisible", false);
    },
    // 手工录入通过身份证号获取信息
    prescriptionNumber() {
      getPatientInfoByIdCard({ idenno: this.inputIdenno }).then((res) => {
        if (res.data.code === 200 && res.data.data != null) {
          const resobj = res.data.data;
          const str = resobj.birthday.split(" ");
          this.ruleForm = {
            name: resobj.name === null ? "" : resobj.name, // 患者名称
            sex: resobj.sex === null ? "" : resobj.sex, // 性别编码 1男 2女
            paykindCode: resobj.memo, // 结算类型
            birthday: 
              resobj.birthday === null ? "" : str[0] + "T" + str[1] + ".000Z", // 出生日期
            idenNo: resobj.idenno === null ? "" : resobj.idenno, // 身份证
            counCode: resobj.counCode === null ? "" : resobj.counCode, // 国籍
            dist: resobj.dist === null ? "" : resobj.dist, // 籍贯
            home: resobj.home === null ? "" : resobj.home, // 现住址
            homeTel: resobj.homeTel === null ? "" : resobj.homeTel, // 联系电话
            linkmaName: resobj.linkmaName === null ? "" : resobj.linkmaName, // 联系人
            linkmanAdd: resobj.linkmanAdd === null ? "" : resobj.linkmanAdd, // 联系人地址
            linkmanTel: resobj.linkmanTel === null ? "" : resobj.linkmanTel, // 联系人电话
            relaCode: resobj.relaCode === null ? "" : resobj.relaCode, // 与联系人关系
            workName: resobj.workName === null ? "" : resobj.workName, // 工作单位
            mcardNo: resobj.mcardNo === null ? "" : resobj.mcardNo, // 社保编号
            pactCode: resobj.pactCode === null ? "" : resobj.pactCode, // 合同单位
            profCode: resobj.profCode === null ? "" : resobj.profCode, // 职务编码
          }; // 手工录入患者信息表单
        } else {
          this.$message.error("找不到此患者信息");
          this.$refs.ruleForm.resetFields();
          this.$refs.hospitalizationForm.resetFields();
        }
      });
    },
    // 手动录入提交
    submitForm(ruleForm, hospitalizationForm) {
      const p1 = new Promise((resolve, reject) => {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            resolve();
          }
        });
      });

      const p2 = new Promise((resolve, reject) => {
        this.$refs[hospitalizationForm].validate((valid) => {
          if (valid) {
            resolve();
          }
        });
      });

      Promise.all([p1, p2]).then(() => {
        // 表单验证通过，写业务代码
        if (this.hospitalizationForm.ryfs === "1") {
          this.hospitalizationForm.ifEmergency = "1"; // 是否急
        } else if (this.hospitalizationForm.ryfs === "2") {
          this.hospitalizationForm.ifWalk = "1"; // 步行
        } else if (this.hospitalizationForm.ryfs === "3") {
          this.hospitalizationForm.ifChair = "1"; // 抬椅
        } else if (this.hospitalizationForm.ryfs === "4") {
          this.hospitalizationForm.ifAmbulance = "1"; // 病车
        }
        delete this.hospitalizationForm.ryfs;
        const arr = this.hospitalizationForm.deptCode&&this.hospitalizationForm.deptCode.split("-")||[];
        const arr1 = this.hospitalizationForm.diagCode&&this.hospitalizationForm.diagCode.split("-")||[];
        const params = Object.assign(
          {},
          this.ruleForm,
          this.hospitalizationForm
        );
        if(this.hospitalizationForm.preOpcode){
          params.preOpname = this.nameList.find(item => item.preOpcode === this.hospitalizationForm.preOpcode)?.preOpname
        }
        (params.operCode = this.urlParam?.jobnumber), //从地址栏中获取jobnumber
          (params.deptName = arr.length === 4 ? `${arr[1]}-${arr[2]}` : arr[1]);
        params.deptCode = arr[0];
        params.sdeptCode = arr[3] ? arr[3] : arr[2];
        // const zd2name = this.getditoname(this.hospitalizationForm.diagCode2)|| ''
        // const zd3name = this.getditoname(this.hospitalizationForm.diagCode3)|| ''
        params.diagCode = arr1[0]||[];
        params.diagName = arr1[1]||[];
        params.diagCode2 =
          (this.hospitalizationForm.diagCode2 &&
            this.hospitalizationForm.diagCode2.split("-")[0]) ||
          "";
        params.diagCode3 =
          (this.hospitalizationForm.diagCode3 &&
            this.hospitalizationForm.diagCode3.split("-")[0]) ||
          "";
        params.diagName2 =
          (this.hospitalizationForm.diagCode2 &&
            this.hospitalizationForm.diagCode2.split("-")[1]) ||
          "";
        params.diagName3 =
          (this.hospitalizationForm.diagCode3 &&
            this.hospitalizationForm.diagCode3.split("-")[1]) ||
          "";
        params.birthday =this.ruleForm.birthday;
        params.preDate = this.hospitalizationForm.preDate
          params.medicalGroupCode = this.hospitalizationForm.medicalGroupCode
          params.medicalGroupName = this.hospitalizationSelect.professionalList.find(item => item.medicalGroupCode === this.hospitalizationForm.medicalGroupCode)?.medicalGroupName
          params.medicalGroupLeaderCode = this.hospitalizationForm.medicalGroupLeaderCode
          params.medicalGroupLeaderName = this.hospitalizationSelect.medicalGroupList.find(item => item.medicalGroupLeaderCode === this.hospitalizationForm.medicalGroupLeaderCode)?.medicalGroupLeaderName
          console.log('params', params)
          const val = {
            idenNo: this.ruleForm.idenNo,
            predateString: this.hospitalizationForm.preDate
          }
          getReadmissionDays(val).then(res => {
            if (res.data.code === 200) {
              if (res.data.data === true) {
                this.getAddEdit(params)
              } else {
                this.$confirm({
                  content: res.data.data,
                  title: '确认信息',
                  okFn: (res) => {
                    this.getAddEdit(params)
                  },
                  cancelFn: (res) => {
                    console.log('res', res);
                    
                  }
                })
              }
            }
          })
          
      });
    },
    getAddEdit(params) {
      if (this.ManualInputEditpreId === "") {
          insertIiPrepayin(params).then((res) => {
            if (res.data.code === 200) {
              this.$message.success(res.data.data);
              this.resetForm();
              this.handleCancel();
              this.$emit("handleFresh");
            } else {
              this.$message.error(res.data.message);
            }
          });
        } else {
          params.birthday = this.ruleForm.birthday
          params.preId = this.ManualInputEditpreId;
          editIiPrepayin(params).then((res) => {
            if (res.data.code === 200) {
              this.$message.success(res.data.data);
              this.resetForm();
              this.handleCancel();
              this.$emit("handleFresh");
            } else {
              this.$message.error(res.data.message);
            }
          });
        }
    },
    getditoname(value) {
      return this.hospitalizationSelect.diagCodeoptions.find(
        (item) => item.value === value
      )?.label;
    },
    // 取消清空手工录入表单
    resetForm() {
      this.$emit("manual", { manualInputVisible: false });
      this.inputIdenno = "";
      this.$refs.ruleForm.resetFields();
      this.$refs.hospitalizationForm.resetFields();
      this.idenNodisabled = false;
      this.$emit("update:ManualInputEditpreId", "");
      // this.hospitalizationSelect.predoctCodeoptions = []
    },
    // 根据科室查病区
    deptChange(val, areaCode) {
      this.newCode = val.substring(0, val.indexOf("-"))
      //   this.hospitalizationForm.predoctCode = "";
      if (val) {
        this.hospitalizationForm.nurseCellCode = "";
        this.hospitalizationForm.medicalGroupCode = ''
      }
      this.hospitalizationSelect.inpatientAreaoptions = [];
      this.hospitalizationSelect.predoctCodeoptions = [];

      this.hospitalizationSelect.professionalList = []
      this.arr = this.hospitalizationForm.deptCode&&this.hospitalizationForm.deptCode.split("-")||[];
      getPatientAreaDTO({ deptCode: this.arr[0], areaCode: areaCode }).then(
        (res) => {
          if (res.data.code === 200) {
            let dats = {
              label: "",
              value: "",
            };
            res.data.data.forEach((element) => {
              dats = {
                label: element.areaName,
                value: element.areaId,
              };
              this.hospitalizationSelect.inpatientAreaoptions.push(dats);
            });
          }
        }
      );
      this.getmedicalGroupList(this.arr[0])
    },
    getmedicalGroupList(i) {
      getmedicalGroup({ deptCode: i }).then(res => {
        if (res.data.code === 200) {
          this.hospitalizationSelect.professionalList = res.data.data
        }
      })
    },

    // 专业组
    medicalGroupChange(val) {
      this.hospitalizationForm.medicalGroupLeaderCode = ''
      this.hospitalizationSelect.medicalGroupList = []
      getmedicalGroupLeader({ medicalGroupCode: val, deptCode: this.arr[0] }).then(res => {
        if (res.data.code === 200) {
          this.hospitalizationSelect.medicalGroupList = res.data.data
        }
      })
    },
    // 根据病区选择床号
    wardChange(row) {
      this.hospitalizationForm.bedNo = ''
      this.hospitalizationSelect.bedNumber = []
      getKongBedList({ nurseCellCode: row }).then(res => {
        if (res.data.code === 200) {
          this.hospitalizationSelect.bedNumber = res.data.data
        }
      })
    },

    // 手工录入搜索医生
    remoteMethod(val) {
      this.hospitalizationSelect.predoctCodeoptions = [];
      const arr = this.hospitalizationForm.deptCode.split("-");
      getDoctList({ deptCode: "", name: val }).then((res) => {
        if (res.data.code === 200) {
          let dats = {
            label: "",
            value: "",
          };
          res.data.data.forEach((element) => {
            dats = {
              label: element.name,
              value: element.id,
            };
            this.hospitalizationSelect.predoctCodeoptions.push(dats);
          });
        }
      });
    },
    // 手工录入获取诊断列表
    DisgnosisChange(val) {
      this.hospitalizationSelect.diagCodeoptions = [];
      getDisgnosis({ name: val }).then((res) => {
        if (res.data.code === 200) {
          let dats = {
            label: "",
            value: "",
          };
          res.data.data.forEach((element, index) => {
            dats = {
              label: element.name,
              value: element.id + "-" + element.name,
            };
            this.hospitalizationSelect.diagCodeoptions.push(dats);
          });
        }
      });
    },
    getDisgnosisData() {
      this.hospitalizationSelect.diagCodeoptions = [];
      getDisgnosis({ name: "" }).then((res) => {
        if (res.data.code === 200) {
          let dats = {
            label: "",
            value: "",
          };
          res.data.data.forEach((element, index) => {
            dats = {
              label: element.name,
              value: element.id + "-" + element.name,
            };
            this.hospitalizationSelect.diagCodeoptions.push(dats);
          });
        }
      });
    },
    // 字典表
    dictionaryList() {
      getBaseDictByIiPrepayin().then((res) => {
        if (res.data.code === 200) {
          res.data.data.insource.forEach((element) => {
            const dats = {
              label: element.name,
              value: element.id,
            };
            this.hospitalizationSelect.daySurgeryoptions.push(dats);
          });
          res.data.data.relative.forEach((element) => {
            const dats = {
              label: element.name,
              value: element.id,
            };
            this.hospitalizationSelect.relativeoptions.push(dats);
          });
          res.data.data.paykinds.forEach((element) => {
            const dats = {
              label: element.name,
              value: element.id,
            };
            this.hospitalizationSelect.paykindCodeoptions.push(dats);
          });
        }
      });
    },
    getDept() {
      const parms = {
        districtCodeList: ["1", "2"],
      };
      getDeptList(parms).then((res) => {
        if (res.data.code === 200) {
          let dats = {
            label: "",
            value: "",
          };
          res.data.data.forEach((element) => {
            dats = {
              label: element.deptName,
              value:
                element.deptCode +
                "-" +
                element.deptName +
                "-" +
                element.sdeptCode,
            };
            this.hospitalizationSelect.departmentoptions.push(dats);
            // console.log('111111111111111', this.hospitalizationSelect.departmentoptions)
          });
        }
      });
    },
    // 手工录入编辑
    ManualInputEdit(id) {
      this.idenNodisabled = true;
      getThisIiPrepayin({ id: id }).then((res) => {
        if (res.data.code === 200) {
          const resobj = res.data.data;
          this.editnurseCellCode = resobj.nurseCellCode;
          // this.$nextTick(() => {
          //   this.deptChange("", resobj.nurseCellCode);
          //   this.remoteMethod(resobj.predoctCode);
          //   this.DisgnosisChange(resobj.diagName);
          // });
          this.$nextTick(() => {
            this.hospitalizationForm = {
              deptCode:
                resobj.deptCode +
                "-" +
                resobj.deptName +
                "-" +
                resobj.sdeptCode, // 住院科室
              deptName: resobj.deptName === null ? "" : resobj.deptName, // 科室名称
              sdeptCode: resobj.sdeptCode === null ? "" : resobj.deptName, // 预约大科
              nurseCellCode:
                resobj.nurseCellCode === null ? "" : resobj.nurseCellCode, // 病区?
              bedNo: resobj.bedNo === null ? "" : resobj.bedNo, // 床号
              preDate: resobj.predate === null ? "" : resobj.predate, // 期望入院时间
              predoctCode:
                resobj.predoctCode === null ? "" : resobj.predoctCode, // 主管医生?
              diagCode: resobj.diagCode + "-" + resobj.diagName, // 诊断编码
              diagName: resobj.diagName === null ? "" : resobj.diagName, // 诊断名称
              ifRjbf: resobj.ifRjbf === null ? "0" : resobj.ifRjbf, // 是否日间手术
              inState: resobj.inState === null ? "" : resobj.inState, // 入院情况
              patientTag: resobj.patientTag === null ? "" : resobj.patientTag, // 院前准备
              ifInhosPlan:
                resobj.ifInhosPlan === null ? "0" : resobj.ifInhosPlan, // 是否31日内非计划再次入院
              ifInhosExternal:
                resobj.ifInhosExternal === null ? "0" : resobj.ifInhosExternal, // 是否外伤入院
              prepayCost:
                resobj.prepayCost === null ? "1000" : resobj.prepayCost, // 预交金
              ryfs:
                resobj.ifWalk === "1"
                  ? "2"
                  : resobj.ifAmbulance === "1"
                  ? "4"
                  : resobj.ifChair === "1"
                  ? "3"
                  : resobj.ifEmergency === "1"
                  ? "1"
                  : "2", // 入院方式
              ifWalk: "0", // 步行2
              ifAmbulance: "0", // 病车4
              ifChair: "0", // 抬椅3
              ifEmergency: "0", // 是否急1
              beizhu: resobj.beizhu === null ? "" : resobj.beizhu, // 备注
              preOpcode:resobj.preOpcode, // 手术名称
              preOpclass:resobj.preOpclass, // 手术级别
              ynNewtech:resobj.ynNewtech, // 是否为新技术
              ynOtherward:resobj.ynOtherward, // 是否跨科收治
              bednoWangted:resobj.bednoWangted, // 意向床号
              medicalGroupCode: resobj.medicalGroupCode,
              medicalGroupName: resobj.medicalGroupName,
              medicalGroupLeaderCode: resobj.medicalGroupLeaderCode,
              medicalGroupLeaderName: resobj.medicalGroupLeaderName,
            }; // 手工录入住院信息表单
            this.newCode = resobj.deptCode
            if(resobj.preOpcode){
              this.handleRemoteName(preOpname)
            }
            // console.log("----------------------", this.hospitalizationForm);
          });
          // // this.$nextTick(() => {
            this.deptChange("", resobj.nurseCellCode);
            this.remoteMethod(resobj.predoctCode);
            this.DisgnosisChange(resobj.diagName);
          // // });
  
          this.ruleForm = {
            name: resobj.name === null ? "" : resobj.name, // 患者名称
            sex: resobj.sex === null ? "" : resobj.sex, // 性别编码 1男 2女
            paykindCode: resobj.memo, // 结算类型
            birthday: resobj.birthday === null ? "" : resobj.birthday, // 出生日期
            idenNo: resobj.idenno === null ? "" : resobj.idenno, // 身份证
            counCode: resobj.counCode === null ? "" : resobj.counCode, // 国籍
            dist: resobj.dist === null ? "" : resobj.dist, // 籍贯
            home: resobj.home === null ? "" : resobj.home, // 现住址
            homeTel: resobj.homeTel === null ? "" : resobj.homeTel, // 联系电话
            linkmaName: resobj.linkmaName === null ? "" : resobj.linkmaName, // 联系人
            linkmanAdd: resobj.linkmanAdd === null ? "" : resobj.linkmanAdd, // 联系人地址
            linkmanTel: resobj.linkmanTel === null ? "" : resobj.linkmanTel, // 联系人电话
            relaCode: resobj.relaCode === null ? "" : resobj.relaCode, // 与联系人关系
            workName: resobj.workName === null ? "" : resobj.workName, // 工作单位
            mcardNo: resobj.mcardNo === null ? "" : resobj.mcardNo, // 社保编号
            pactCode: resobj.pactCode === null ? "" : resobj.pactCode, // 合同单位
            profCode: resobj.profCode === null ? "" : resobj.profCode, // 职务编码
            idenType: resobj.idenType === null ? "" : resobj.idenType,
          }; // 手工录入患者信息表单
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.drawer-box {
  padding-top: 15px;
  p {
    width: 100%;
    height: 40px;
    background-color: #f4f8fb;
    color: #555555;
    font-size: 16px;
    line-height: 40px;
    padding: 0 10px;
    box-sizing: border-box;
  }
  .box-information {
    width: 100%;
    box-sizing: border-box;
    display: flex;
    padding: 23px 0px 23px 20px;
    box-sizing: border-box;
    overflow: hidden;
    position: relative;
    .basic-box {
      display: flex;
      flex-wrap: wrap;
      overflow: hidden;
      color: #555555;
      div {
        width: 240px;
        height: 33px;
        line-height: 33px;
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    .more-text {
      width: 100px;
      position: absolute;
      right: 10px;
      bottom: 20px;
      color: rgba(24, 144, 255, 100);
      cursor: pointer;
      font-size: 14px;
    }
  }
  .demo-ruleForm {
    box-sizing: border-box;
    padding: 15px 18px 0 11px;
    .formitem {
      display: flex;
      justify-content: space-between;
      /deep/ .el-input__suffix-inner {
        pointer-events: all;
        display: flex;
        align-items: center;
        height: 100%;
      }
    }
    /deep/ .el-form-item__error {
      padding: 0 !important;
    }
    .el-form-item {
      margin-bottom: 10px !important;
    }
    /deep/ .el-form-item__label {
      color: #555555 !important;
    }
  }
  .el-table /deep/.cell {
    font-size: 14px !important;
  }
  .el-button /deep/ span {
    font-size: 14px !important;
  }
}
</style>
